package com.action.ui.fragment;


import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.action.childbase.ChildBaseFragment;
import com.action.entity.Redpacket;
import com.action.entity.UserRedpacket;
import com.action.service.pressenter.RedpakcetPressenter;
import com.base.app_core.View.xrecyclerview.RecyclerAdapter;
import com.base.app_core.View.xrecyclerview.RecyclerViewHolder;
import com.base.app_core.model.Paginated;
import com.base.app_core.util.AppConstants;
import com.base.app_core.util.XYBAUtils;
import com.xinglai.yun.R;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class RedDetailUserListFrag extends ChildBaseFragment<RedpakcetPressenter> {


    @BindView(R.id.red_user_tag)
    TextView redUserTag;

    @BindView(R.id.red_user_list)
    XRecyclerView redUserList;

    private RedUserAdapter redUserAdapter;
    private Redpacket repacket;
    private Boolean hasMore = true;

    public void setRepacket(Redpacket repacket) {
        this.repacket = repacket;
        if (null != redUserTag) {
            if (repacket.getType() == 2) {
                redUserTag.setVisibility(View.GONE);
            } else {
                redUserTag.setVisibility(View.VISIBLE);
                String userTotalMoney = repacket.getAccount().setScale(2, RoundingMode.DOWN).toPlainString();
                redUserTag.setText("已领取" + String.valueOf(repacket.getConsumeNum() == null ? 0 : repacket.getConsumeNum())
                        + "/" + repacket.getNum() + ",共" + userTotalMoney + "元（" + repacket.getUserPercent() +
                        "奖励给用户）");
            }
        }
    }


    public RedUserAdapter getRedUserAdapter() {
        return redUserAdapter;
    }

    public static class RedUserAdapter extends RecyclerAdapter<UserRedpacket, RedUserAdapter.ViewHolder> {

        @Override
        protected void fillData(UserRedpacket item, ViewHolder holder) {
            XYBAUtils.displayCircleImage(holder.itemView.getContext(), item.getWxIcon(), holder.userImage);
            holder.recvName.setText(item.getWxNickname());
            holder.recvTime.setText(item.getRecTime());
            holder.recvMoney.setText(new BigDecimal(item.getMoney()).setScale(4, RoundingMode.DOWN).toPlainString());
            holder.recvHoleCount.setText(item.getStockCount() + "红包股");
        }

        @Override
        protected int getItemLayoutId() {
            return R.layout.unit_user_redpacket;
        }

        public class ViewHolder extends RecyclerViewHolder {
            @BindView(R.id.user_image)
            ImageView userImage;
            @BindView(R.id.recv_name)
            TextView recvName;
            @BindView(R.id.recv_time)
            TextView recvTime;
            @BindView(R.id.recv_money)
            TextView recvMoney;
            @BindView(R.id.recv_hole_count)
            TextView recvHoleCount;

            public ViewHolder(View itemView) {
                super(itemView);
            }
        }
    }

    @Override
    protected void initView() {
        super.initView();
        redUserAdapter = new RedUserAdapter();
        redUserList.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        redUserList.setAdapter(redUserAdapter);
        redUserList.setPullRefreshEnabled(false);
        redUserList.setLoadingMoreEnabled(true);
        redUserList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                if (!hasMore) {
                    return;
                }
                pageNo += 1;
                fetchUserList();
                redUserList.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (redUserList == null) {
                            return;
                        }

                        redUserList.loadMoreComplete();
                    }
                }, AppConstants.Const.REFRESHTIME);
            }
        });
        redUserList.setEmptyView(getEmptyView(redUserList, "还没有用户领取红包", "", null));
    }

    @Override
    protected void setupComponent() {
        super.setupComponent();
        getChildViewComponent().inject(this);
    }

    private int pageNo = 1;
    private String sn;

    public void setRedpacketSn(String sn) {
        this.sn = sn;
    }

    @Override
    protected void loadData() {
        super.loadData();
        fetchUserList();
    }

    public void fetchUserList() {
        if (null != mPresenter) {
            mPresenter.fetchRedUserList(pageNo, sn);
        }
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public RedDetailUserListFrag() {

    }

    @Override
    protected boolean needShowError() {
        return true;
    }

    @Override
    protected boolean isNeedShowLoading() {
        return true;
    }

    @Override
    public <T> void notifyData(T object, Class tClass, Paginated paginated) {
        super.notifyData(object, tClass, paginated);
        if (object instanceof Collection) {
            if (tClass == UserRedpacket.class) {
                if (redUserAdapter == null || redUserList == null) {
                    return;
                }

                List<UserRedpacket> userRedpackets = new ArrayList<>();
                userRedpackets.addAll((List<UserRedpacket>) object);
                if (pageNo == 1) {
                    redUserAdapter.clear();
                }
                redUserAdapter.addAll(userRedpackets);
                redUserAdapter.notifyDataSetChanged();
                if (redUserList != null) {
                    hasMore = paginated.getHasMore();
                    redUserList.setNoMore(!hasMore);
                    redUserList.setLoadingMoreEnabled(hasMore);
                }

            }
        }
        setState(AppConstants.STATE_SUCCESS);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_red_detail_user_list;
    }
}
